<?php

function acpu_ttl($key)
{		$info=apcu_key_info($key);
		if(empty($info)){
			return 0;
		}
		$ttl=$info['ttl']-(time()-$info['creation_time']);
		return $ttl>0 ? $ttl :0;
}
function mysql_like_quote($str)
{
		return strtr($str, array('%' => '\%','\\' => '\\\\', '_' => '\_', '\_' => '_', "'" => "\'", '"' => '\"'));
}
//净化md5
function md5_clear(string $md5): string
{
	return substr(preg_replace('/\W/','',$md5),0,60);
}
//取access_token
function access_token(){
	return app('token')->getId();
}
//取access_token过期  ttl
function access_token_expire(){
	return app('token')->getExpire();
}
/**
 * 多个空格合成一个后trim
 * @param string $str
 * @return string
 */
function spacesToOne($str,$space=' '){
		return trim(preg_replace('/(\s|　|\xc2\xa0)+/',$space,$str));
}

/**
 * 字节格式化 把字节数格式为 B K M G T 描述的大小
 * @return string
 */
function byte_format($size, $dec=2) {
		$a = array("B", "KB", "MB", "GB", "TB", "PB");
		$pos = 0;
		while ($size >= 1024) {
				$size /= 1024;
				$pos++;
		}
		return round($size,$dec)." ".$a[$pos];
}
/**
 * 检查字符串是否是UTF8编码
 * @param string $string 字符串
 * @return Boolean
 */
function is_utf8($string) {
		return function_exists('mb_detect_encoding') ? mb_detect_encoding($string)=='UTF-8' :
				preg_match('%^(?:
		 [\x09\x0A\x0D\x20-\x7E]            # ASCII
		 | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
		 |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
		 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
		 |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
		 |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
		 | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
		 |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
	)*$%xs', $string);
}
//获取已过时间
function pass_date($time){
		$time_span = NOW_TIME - $time;
		if($time_span>3600*24*365){
				$time_span_lang = date('Y-m-d',$time);
		}elseif($time_span>3600*24*30){
				$time_span_lang = date('Y-m-d',$time);
		}elseif($time_span>3600*24){//天
				$time_span_lang = round($time_span/(3600*24))."天前";
		}elseif($time_span>3600){//小时
				$time_span_lang = round($time_span/(3600))."小时前";
		}elseif($time_span>60){//分
				$time_span_lang = round($time_span/(60))."分钟前";
		}else{//秒
				$time_span_lang = "刚刚";
		}
		return $time_span_lang;
}
/**
 * 加https://
 * @param   string  $url  参数字符串，一个urld地址,对url地址进行校正
 * @return  返回校正过的url;
 */
function add_http($url , $check = 'https://')
{
		if (stripos($url, 'http://' ) === false && stripos($url, 'https://' ) === false){
				$url = $check . $url;
		}
		return $url;
}
/**
 * 返回正确的status值      空串返回null
 * @param   string  $url  参数字符串，一个urld地址,对url地址进行校正
 * @return  返回校正过的url;
 */
function get_status($str){
	$status=intval($_REQUEST[$str]);
	if(empty($status) && trim($_REQUEST[$str])!=='0'){
		$status=null;
	}
	return $status;
}

function htmlquotes($str,$decode=false): string
{
	return $decode ? htmlspecialchars_decode($str,ENT_QUOTES) : htmlspecialchars($str, ENT_QUOTES);
}

/**
 * 转为浮点数*100四舍五入  便于比较大小  失败返回0
 *
 * @access      public
 * @param       mixed    $num
 * @return      $num*100
 */
function int100($num){
	return intval(round(floatval($num)*100));
}
function int10($num){
	return intval(round(floatval($num)*10));
}
/**
 * 转为0或1
 */
function int01($num){
	return intval($num) ? 1 :0;
}
/**
 * 小于0转为0
 */
function minus0($num){
	$num=floatval($num);
	return $num<0?0:$num;
}
function div100($num){
	return intval($num)/100;
}
function plus($num){
	return $num>0?'+'.$num:$num;
}
/**
 * 格式化价格
 *
 * @access  public
 * @param   float  $price  价格
 * @param   bool   $change_price  是否格式化价格
 * @param   bool   $div100  除100
 * @return  string
 */
function price_format($price, $change_price = true,$div100=true)
{
	if(!$price)
	{
		$price=0;
	}elseif($div100) $price/=100;

	if ($change_price)
	{
		switch(C('price_format'))
		{
			case 0:// 四舍五入，保留2位
				$price = number_format($price, 2, '.', '');
				break;
			case 1: // 保留不为 0 的尾数
				$price = preg_replace('/(.*)(\\.)([0-9]*?)0+$/', '\1\2\3', number_format($price, 2, '.', ''));

				if (substr($price, -1) == '.')
				{
					$price = substr($price, 0, -1);
				}
				break;
			case 2: // 不四舍五入，保留1位
				$price = substr(number_format($price, 2, '.', ''), 0, -1);
				break;
			case 3: // 直接取整
				$price = intval($price);
				break;
			case 4: // 四舍五入，保留 1 位
				$price = number_format($price, 1, '.', '');
				break;
			case 5: // 先四舍五入，不保留小数
				$price = round($price);
				break;
		}
		return sprintf(C('currency_format'), $price);
	}
	else
	{
		return number_format($price, 2, '.', '');
	}

}
function move_file($tmp_file){
	$uploads=public_path('uploads');
	$i=$uploads.'tmp/'.$tmp_file;
	if($tmp_file && file_exists($i)){
		$j=$uploads.'imgs/'.$tmp_file;
		make_dir(dirname($j));
		return rename($i,$j);
	}
	return false;
}
/**
 *检查$_FILES['上传某name']有没有上传成功
 * @param   array  $files=$_FILES['上传某name']
 * @return  bool
 */
function chk_files($files){
	if (empty($files)){
		return false;
	}
	if ((isset($files['error']) && $files['error'] == 0) || (!isset($files['error']) && isset($files['tmp_name']) && $files['tmp_name'] != 'none')){
		return true;
	}
	return false;
}

/**
 * 检查目标文件夹是否存在，如果不存在则自动创建该目录
 *
 * @access      public
 * @param       string      folder     目录路径。
 *
 * @return      bool
 */
function make_dir($folder,$mode=0755)
{
	$reval = true;
	if (!is_dir($folder)){
		$reval = mkdir($folder,$mode,true);
	}
	return $reval;
}

/**
 * 获取唯一文件名
 *
 * @access      public
 * @param       string      folder				目录路径   D:/www/imgs/
 * @param       string      $ext				  扩展名	 .png
 *
 * @return      bool
 */
function random_name($folder,$ext){
	do{
		$filename=uniqid().$ext;
	}while(is_file($folde.$filename));
	return $filename;
}